package com.four.basicinfo.controller;


import com.four.basicinfo.code.ResponseData;
import com.four.basicinfo.dao.InformationDao;
import com.four.basicinfo.dao.UserDao;
import com.four.basicinfo.entity.Information;
import com.four.basicinfo.entity.Parents;
import com.four.basicinfo.entity.User;
import com.four.basicinfo.service.UserService;
import com.four.basicinfo.until.JwtUtils;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/sys/user")
public class UserController extends BaseController{

    @Autowired
    UserService userService;
    @Autowired
    InformationDao informationDao;





    @GetMapping("/login")
    public ResponseData<Object> login(String username, String password) {


        User user = userService.login( username, password );
        if (user != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put( "id",user.getId() );
            claims.put( "username",user.getUsername() );
            String jwt=JwtUtils.generateJwt(claims);
            ResponseData<Object> responseData = new ResponseData<>();
            responseData.setErrorCode( String.valueOf( 200 ) );
            responseData.setMsg(jwt);
            responseData.setData( user );
            return responseData;
        }else {
            return returnObject( null );
        }

    }



    @PostMapping("/add")
    public ResponseData<Boolean> add(User user){
        log.info( "用户注册 {}",user );
        Boolean adduser = userService.adduser( user );
        return returnBoolean( adduser );
    }

    @GetMapping("/gets")
    public ResponseData<Object> getUser(User user){
        List<User> list=userService.getUsers(user);
        return returnObject(list);
    }

    @PutMapping("/update")
    public ResponseData<Boolean> update(Integer id,String role,String islock){
        Boolean b=userService.updateById(id,role,islock);
        return returnBoolean(b);
    }

    @PutMapping("/updatepass")
    public ResponseData<Boolean> updatepass(String username, String password){
        Boolean b=userService.updatepass(username,password);
        return returnBoolean(b);
    }



    /*@GetMapping("/gets")
    public  ResponseData<Object> getUsers( @RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int pageSize){
        PageResult users = userService.getUsers( page, pageSize );
        return returnObject( users );
    }
*/


    @DeleteMapping("/del/{id}")
    public ResponseData<Boolean> delUserById(@PathVariable Integer id){
        return returnBoolean( userService.delById(id) ) ;
    }

    //选择合同工姓名
    @GetMapping("/get")
    public List<String> selectName(){
        return informationDao.selectName();
    }

    //根据姓名 查询 信息
    @GetMapping("/info")
    public Information selectInfo(String name){
        if(name==null){
            return null;
        }
        return informationDao.selectNumber(name);
    }

}
